﻿$(document).ready(function () {
    $('#fr-searchproductsbynameorid').submit(function () {
        var serializedForm = $(this).serialize();
        App.blockUI($('.lstFoundProducts'), false);
        var results = $("#lstFoundProducts");
        $.ajax({
            type: 'POST',
            url: '/products/SearchProductsByNameOrId',
            data: serializedForm,
            success: function (data) {
                results.html('');
                $.each(data, function (id, option) {
                    results.append($('<option></option>').val(option.id).html(option.name));
                });
                App.unblockUI($('.lstFoundProducts'));
            },
            error: function () {
                alert('search fail!');
                App.unblockUI($('.lstFoundProducts'));
            }
        });
        return false;
    });

    $('#fr-searchoptionoroptionname').submit(function () {
        var serializedForm = $(this).serialize();
        App.blockUI($('.lstFoundProductOptions'), false);
        var results = $("#lstFoundProductOptions");
        $.ajax({
            type: 'POST',
            url: '/products/searchoptionoroptionname',
            data: serializedForm,
            success: function (data) {
                results.html('');
                $.each(data, function (id, option) {
                    results.append($('<option></option>').val(option.id).html(option.name));
                });
                App.unblockUI($('.lstFoundProductOptions'));
            },
            error: function () {
                alert('search fail!');
                App.unblockUI($('.lstFoundProductOptions'));
            }
        });
        return false;
    });

    $(".btn-printdocument").click(function () {
        window.print();
    });

    $('#fr-searchspreadsheet').submit(function () {
        var serializedForm = $(this).serialize();
        App.blockUI($('body'), false);
        $.ajax({
            type: 'POST',
            url: '/products/searchspreadsheet',
            cache: false,
            data: serializedForm,
            success: function (data) {
                $(".spreadsheet-result").html(data);
                App.unblockUI($('body'));
                InitDatatable();
                InitTableSelected();
                if ($(".cbarchived").is(":checked")) {
                    $(".part-selected").val('UnArchive');
                } else {
                    $(".part-selected").val('Delete selected');
                }
            },
            error: function () {
                alert('search fail!');
                App.unblockUI($('body'));
            }
        });
        return false;
    });

    $("#fr-moderatereviewfilter").submit(function () {

        var serializedForm = $(this).serialize();
        App.blockUI($('body'), false);
        $.ajax({
            type: 'POST',
            url: '/products/moderatereviewfilter',
            cache: false,
            data: serializedForm,
            success: function (data) {
                $(".reviewscontent").html(data);
                App.unblockUI($('body'));
                InitDatatable();
                InitTableSelected();
                var height = $(window).height() - 250;
                $(".reviewscontent table").attr("data-height", height);
            },
            error: function () {
                alert('search fail!');
                App.unblockUI($('body'));
            }
        });
        return false;
    });
    $("#frm-searchproduct").submit(function () {
        if ($('#nameproduct').val() == "" || $('#nameproduct').val() == null) {
            alert("Please enter search product data!");
            return false;
        }
        var serializedForm = $(this).serialize();
        App.blockUI($('#tableProductSearch'), false);
        $.ajax({
            type: 'POST',
            url: '/products/SearchProductOption',
            cache: false,
            data: serializedForm,
            success: function (data) {
                $("#tableProductSearch").html(data);
                App.unblockUI($('#tableProductSearch'));
                InitTableSelected();
            },
            error: function () {
                alert('search fail!');
                App.unblockUI($('#tableProductSearch'));
            }
        });
        return false;
    });

    $("#frm-Review").submit(function () {
        App.blockUI($('body'));
        if ((document.getElementsByName("TasksToBeCreated")[0].checked == false && document.getElementsByName("TasksToBeCreated")[1].checked == false) || document.getElementsByName("OrderRef")[0].value == "") {
            if (!confirm("Tasks will not be created, do you want to save anyway?")) {
                App.unblockUI($('body'));
                return false;
            }
        }

        $.ajax({
            type: 'POST',
            url: '/products/Review',
            data: $(this).serialize(),
            success: function (data) {
                alert(data.message);
                if (data.success == 1) {
                    window.opener.moderatereviewfilter();
                    //    document.forms["#fr-moderatereviewfilter"].submit();
                    window.close();
                }
            },
            error: function () {
                App.unblockUI($('body'));
            }
        });
        App.unblockUI($('body'));
        return false;
    });

    $('.TasksToBeCreated').click(function () {

        if ($(this).is(":checked")) {
            $('.TasksToBeCreated').prop('checked', false);
            $(this).prop('checked', true);
        }

    });
    $("#fr-productoptprice").submit(function () {
        App.blockUI($('body'), false);
        $(".productoptionbreakdown").html("");
        $.ajax({
            type: 'POST',
            url: '/products/productoptpricefilter',
            data: $(this).serialize(),
            success: function (data) {
                $('.productoptionresults').html(data);
                InitDatatable();
                InitTableSelected();
                var value = $("#suggestretail").val();
                if (value.length > 0) {
                    $("#btnReset").show();
                    $("#btnSet").hide();
                    $(".sugg").css("color", "red");
                }
                App.unblockUI($('body'));
            },
            error: function () {
                App.unblockUI($('body'));
            }
        });

        return false;
    });
    $("#fr-promotionaudit").submit(function () {
        var serializedForm = $(this).serialize();
        App.blockUI($('body'), false);
        $.ajax({
            type: 'POST',
            url: '/products/promotionauditfilter',
            cache: false,
            data: serializedForm,
            success: function (data) {
                $(".promotionauditable").html(data);
                App.unblockUI($('body'));
                //InitDatatable();
                InitAjaxTable($('.promoaudit-table'));
                InitTableSelected();
            },
            error: function () {
                alert('search fail!');
                App.unblockUI($('body'));
            }
        });
        return false;
    });

    $('#fr-openwebsite').submit(function () {
        App.blockUI($('body'));
        $.ajax({
            type: 'POST',
            url: '/products/OpenWebsites',
            data: $(this).serialize(),
            success: function (data) {
                alert(data.message);
                if (data.success == 1) {
                    window.opener.$('#twebsite').html(data.twebsite);
                    window.opener.InitTableSelected();
                    window.close();
                }

            },
            error: function () {
                App.unblockUI($('body'));
            }
        });
        App.unblockUI($('body'));
        return false;
    });


    $('#fr-openparentcate').submit(function () {
        App.blockUI($('body'));
        $.ajax({
            type: 'POST',
            url: '/products/OpenParentCate',
            data: $(this).serialize(),
            success: function (data) {
                alert(data.message);
                window.opener.LoadParentCate(data);
                window.close();

            },
            error: function () {
                App.unblockUI($('body'));
            }
        });
        App.unblockUI($('body'));
        return false;
    });

    $('#fr-openchildcate').submit(function () {
        App.blockUI($('body'));
        $.ajax({
            type: 'POST',
            url: '/products/OpenChildCate',
            data: $(this).serialize(),
            success: function (data) {
                alert(data.message);
                window.opener.LoadChildCate(data);
                window.close();

            },
            error: function () {
                App.unblockUI($('body'));
            }
        });
        App.unblockUI($('body'));
        return false;
    });

    $('#fr-openproducts').submit(function () {

        App.blockUI($('body'));
        dataPost = $(this).serialize();
        displayProduct = $('#displayProduct').is(":checked");
        $.ajax({
            type: 'POST',
            url: '/products/openproducts?displayProduct=' + displayProduct,
            data: dataPost,
            success: function (data) {
                if (data.success == 1) {
                    window.opener.LoadProducts(data);
                    if (data.message.length > 2) {
                        alert(data.message);
                    }
                }
                else if (data.success == 2) {
                    if (confirm(data.message)) {
                        $.ajax({
                            type: 'POST',
                            url: '/products/ConfirmRemoveURL?displayProduct=' + displayProduct,
                            data: dataPost,
                            error: function () {
                                App.unblockUI($('body'));
                            }
                        });
                    }
                }
                App.unblockUI($('body'));
            },
            error: function () {
                App.unblockUI($('body'));
            }
        });
        App.unblockUI($('body'));
        return false;
    });

    InitTableSelected();

    $("#txtSearch").bind('keydown', function (e) {
        if (e.keyCode == 13) {
            searchFeature();
        }
    });
    checkFeatureSections();

});

function searchFeature() {
    var value = $("#txtSearch").val();
    $.ajax({
        url: "/products/SearchFeatures?search=" + value,
        type: "POST",
        success: function (data) {
            loadFeatureTable(data);
        }
    });
}
function loadFeatureTable(data) {
    App.blockUI($('body'));
    $("#page-editproductfeatures").html(data);
    if ($('.krmtable').length > 0) {
        $('.krmtable').each(function () {
            LoadDataTable($(this));
        });
    }
    InitTableSelected();
    $("#txtSearch").bind('keydown', function (e) {
        if (e.keyCode == 13) {
            searchFeature();
        }
    });
    checkFeatureSections();
    App.unblockUI($('body'));
}
function checkFeatureSections() {
    $("#feature-section label input.checkbox").change(function () {
        var value = $(this).val();
        var str = $("#featuresection").val();
        if (this.checked) {
            str = str + value + ",";
            $("#featuresection").val(str);
        } else {
            str = str.replace(value + ",", "");
            $("#featuresection").val(str);
        }
    });
}

function FoundProductOptionsSelected(option) {
    if (option != "") {
        $.ajax({
            url: "/products/GetProductOption?optionid=" + option,
            success: function (data) {
                $(".btn-generatedocument").attr("onclick", "productgeneratedocument(" + data.optionid + ")");
                $(".lbProductName").html(data.productid + " - " + data.productname);
                $(".lbProductOption").html(data.optionid + " - " + data.productoption);
            }
        });
    }
}

function productgeneratedocument(optionid) {
    App.blockUI($('.pn-documentgeneration'), false);
    var result = $(".htmlproductdocument");
    $.ajax({
        url: "/products/GeneratePackingList?optionid=" + optionid,
        success: function (data) {
            result.addClass("border-padding10");
            result.html(data);
            App.unblockUI($('.pn-documentgeneration'), false);
        }
    });
}

function spreadsheetselected(partid, partname) {
    App.blockUI($('.associatedproducts-result'), false);
    $.ajax({
        url: "/products/getassociatedproducts?partid=" + partid,
        success: function (data) {
            $(".associatedproducts-result").html(data);
            $(".part-selected").attr("onclick", "spreadsheetpartdelete('" + partid + "','" + partname + "')");
        }
    });
}

function spreadsheetpartdelete(partid, partname) {
    var archive = $(".cbarchived").is(":checked");
    if (archive) {
        App.blockUI($('body'), false);
        $.ajax({
            url: "/products/spreadsheetdeletepart",
            data: { partid: partid, archive: archive },
            cache: false,
            success: function (data) {
                alert(data);
                $('#fr-searchspreadsheet').submit();
            }, error: function () {
                alert("Error");
                App.unblockUI($('body'));
            }
        });
    } else {
        if (confirm('Are you sure you want to delete ' + partname + '?')) {
            App.blockUI($('body'), false);
            $.ajax({
                url: "/products/spreadsheetdeletepart",
                data: { partid: partid, archive: archive },
                cache: false,
                success: function (data) {
                    alert(data);
                    $('#fr-searchspreadsheet').submit();
                }, error: function () {
                    alert("Error");
                    App.unblockUI($('body'));
                }
            });
        }
    }
}

function reviewstorechange(storeid) {
    $.ajax({
        url: "/products/reviewstorechange?storeid=" + storeid,
        cache: false,
        success: function (data) {
            $("#ProductID").html("");
            $.each(data, function (id, option) {
                $("#ProductID").append($('<option></option>').val(option.Id).html(option.Name));
            });
        },
        error: function () {
            alert("Error Store Change!");
        }
    });
}

function productreviewselected(reviewid, name) {
    $(".btn-editreview").attr("onclick", "PopupCenter('/products/review?reviewid=" + reviewid + "',900,650)");
    $(".btn-markasspam").attr("onclick", "markasspamreview('" + reviewid + "','" + name + "')");
    $(".btn-approveanddisplay").attr("onclick", "ApproveAndDisplay('" + reviewid + "','" + name + "')");
    $(".btn-approve").attr("onclick", "Approve('" + reviewid + "','" + name + "')");
    $(".btn-markasunsuitable").attr("onclick", "MarkAsUnsuitable('" + reviewid + "','" + name + "')");
}

function markasspamreview(reviewid, name) {
    if (confirm("Are you sure you wish to mark " + name + "'s review as spam?")) {
        $.ajax({
            url: "/Products/MarkAsSpamReview?reviewid=" + reviewid,
            success: function () {
                alert('Success!');
                $("#fr-moderatereviewfilter").submit();
            }
        });
    }
}
function ApproveAndDisplay(reviewid, name) {
    if (confirm("Are you sure you wish to display " + name + "'s review on the website?")) {
        $.ajax({
            url: "/Products/ApproveAndDisplay?reviewid=" + reviewid,
            success: function () {
                alert('Success!');
                $("#fr-moderatereviewfilter").submit();
            }
        });
    }
}
function Approve(reviewid, name) {
    if (confirm("Are you sure you wish to approve " + name + "'s review?")) {
        $.ajax({
            url: "/Products/Approve?reviewid=" + reviewid,
            success: function () {
                alert('Success!');
                $("#fr-moderatereviewfilter").submit();
            }
        });
    }
}
function MarkAsUnsuitable(reviewid, name) {
    if (confirm("Are you sure you wish to mark " + name + "'s review as unsuitable?")) {
        $.ajax({
            url: "/Products/MarkAsUnsuitable?reviewid=" + reviewid,
            success: function () {
                $("#fr-moderatereviewfilter").submit();
                alert('Success!');
            }
        });
    }
}
function FindRelatedOrder() {
    var email = document.getElementsByName("email")[0].value;
    App.blockUI($('.blockui'));
    $.ajax({
        url: "/products/OrderCollections?email=" + email,
        success: function (data) {
            $('.order-collection').html(data);
            InitDatatable();
            InitTableSelected();
            App.unblockUI($('.blockui'));
        }
    });
    App.unblockUI($('.blockui'));
}

function SetValueOrderRef(OrderRef) {

    $("#hfOrderref").val(OrderRef);
}

function reviewstorechange(storeid) {
    App.blockUI($('body'));
    $.ajax({
        url: "/Products/LoadProducts?storeID=" + storeid,
        success: function (data) {
            $('.Products').html("");
            $.each(data, function (id, option) {
                $('.Products').append($('<option></option>').val(option.Id).html(option.Name));
            });
            App.unblockUI($('body'));
        }
    });
}
function moderatereviewfilter() {
    $("#fr-moderatereviewfilter").submit();
}
function productoptionbreakdowndetail(productoptionid) {
    App.blockUI($('.productoptionbreakdown'), false);
    $.ajax({
        url: "/products/ProductOptionBreakDown?productoptionid=" + productoptionid,
        success: function (data) {
            $('.productoptionbreakdown').html(data);
            InitAjaxTable($('.table-breakdown'));
            App.unblockUI($('.productoptionbreakdown'));
        },
        error: function () {
            App.unblockUI($('.productoptionbreakdown'));
        }
    });

}
function selected(obj) {
    if (obj.className.indexOf("de-selected") > 0) {
        obj.className = obj.className.replace("de-selected", "");
    } else {
        obj.className = obj.className + " de-selected";
    }
}
function submitModal() {
    $('#hd-filter').val("");
    $("#departmentSelected a").each(function (index, element) {
        var value = $('#hd-filter').val();
        value = value + element.innerText + ",";
        $('#hd-filter').val(value);
    });
    if ($('#hd-filter').val().length > 0) {
        $("#btnDepartment").addClass("btn-danger");
    } else {
        $("#btnDepartment").removeClass("btn-danger");
    }
}
function loadDepartment() {
    $("#departmentSelected").html("");
    $('#hd-filter').val("");
    $.ajax({
        type: 'POST',
        url: '/products/loaddepartment',
        success: function (data) {
            $('#department').html('');
            $.each(data, function (id, option) {
                $('#department').append("<a class='span12' rel='" + option + "' onclick='selected(this)'><span>" + option + "<span></a>");
            });
        },
        error: function () {
            alert('search fail!');
        }
    });
}
function setSuggestRetail() {
    var value = $("#suggestretail").val();
    App.blockUI($('body'), false);
    $(".productoptionbreakdown").html("");
    $.ajax({
        type: 'POST',
        url: '/products/setSuggestRetail?value=' + value,
        success: function (data) {
            $("#btnReset").show();
            $("#btnSet").hide();
            $('.productoptionresults').html(data);
            InitDatatable();
            InitTableSelected();
            $(".sugg").css("color", "red");
            App.unblockUI($('body'));
        },
        error: function () {
            App.unblockUI($('body'));
        }
    });
}
function resetSuggestRetail() {
    App.blockUI($('body'), false);
    $("#suggestretail").val("");
    $("#btnReset").hide();
    $("#btnSet").show();
    $(".vsugg").html("0.0");
    $(".sugg").css("color", "");
    App.unblockUI($('body'));
}
function showarchivedchange(checkbox) {
    if (!checkbox.is(":checked")) {
        $(".rownotarchived").hide();
    } else {
        $(".rownotarchived").show();
    }
}

function promotionselected(promotionid) {
    App.blockUI($('.promotiondetail'));
    $.ajax({
        url: "/products/promotiondetail?promotionid=" + promotionid,
        success: function (data) {
            $(".promotiondetail").html(data);
            $.ajax({
                url: "/products/promotionlinksdetail?promotionid=" + promotionid,
                success: function (res) {
                    $(".promotiondetail").append(res);
                    InitDatetimePicker();
                    $("#fr-savepromotion").submit(function () {
                        var serializedForm = $(this).serialize();
                        App.blockUI($('body'), false);
                        $.ajax({
                            type: 'POST',
                            url: '/products/SavePromotion',
                            cache: false,
                            data: serializedForm,
                            success: function () {
                                loadPromotionList();
                                App.unblockUI($('body'));
                            },
                            error: function () {
                                alert('save fail!');
                                App.unblockUI($('body'));
                            }
                        });
                        return false;
                    });
                    InitAjaxTable($(".websitetable"));
                    InitAjaxTable($(".productstable"));
                    InitTableSelected();
                }
            });
        }
    });
}
function changeCatTree(catId) {
    App.blockUI($('#treeview'));
    $.ajax({
        url: "/products/LoadCatTree?catid=" + catId,
        success: function (data) {
            $("#treeview").html(data);
            $("#tree").treeview({
                animated: "fast",
                collapsed: true,
            });
            $("#treeview a").click(function () {
                $("#treeview a").removeClass("tree-selected");
                $(this).addClass("tree-selected");
                var value = $(this).attr("val");
                $('#lblRelatedCat').html(value + " - " + this.innerHTML);
                $('#hdRelCat').val(value);
            });
            App.unblockUI($('#treeview'));
        }
    });
    App.blockUI($('#listview'));
    $.ajax({
        url: "/products/LoadListView?catid=" + catId,
        success: function (data) {
            $("#listview").html(data);
            InitAjaxTable($('.listview-table'));
            InitTableSelected();
            App.unblockUI($('#listview'));
        }
    });
}
function savePromoWebsite() {
    var promId = $('#promotionId').val();
    var storeId = $('#StoreID').val();

    var catId = $('#hdRelCat').val();
    var posi = $('#positionPromo').val();
    var promosite = $('#promositeid').val();
    promosite = promosite != "" ? promosite : "0";
    if (catId == "") {
        alert("Please choose a store!");
        return false;
    }
    $.ajax({
        url: "/products/SavePromotionWebsite",
        type: "POST",
        data: { promId: promId, storeId: storeId, catId: catId, position: posi, PromoSiteId: promosite },
        success: function (data) {
            if (data.success != "0") {
                promotionselected(promId);
                if (data.success == "1") {
                    alert(data.message);
                }
            }

        }
    });
}
function updatePromWebsite(PromSiteId) {
    $.ajax({
        url: "/products/ModalWebsite?proSiteId=" + PromSiteId,
        success: function (data) {
            $('#modalWebsite').html(data);
        },
        error: function () {
            alert("error");
        }
    });
}
function selectList(id) {
    $('#lblRelatedCat').html(id + " - " + $('#tr-' + id).attr("rel"));
    $('#hdRelCat').val(id);
}
function deletePromoSite() {
    var id = $('#promositeid').val();
    var promId = $('#promotionId').val();
    if (id == "") return false;
    $.ajax({
        url: "/products/DeletePromotionSite",
        type: "POST",
        data: { id: id },
        success: function (data) {
            promotionselected(promId);
        },
        error: function () {
            alert("error");
        }
    });
    return false;
}

function promWidget(obj) {
    var widId = obj.id;
    var promId = $('#promotionId').val();
    $.ajax({
        url: "/products/ChangePromotionWidgets",
        type: "POST",
        data: { widId: widId, promId: promId },
        success: function (data) {
            //promotionselected(promId);
        },
        error: function () {
            alert("error");
        }
    });
}
function ShowProductOption(productid) {
    $.ajax({
        url: "/products/ShowProductOption?productid=" + productid,
        success: function (data) {
            var lst = JSON.parse(data);
            var str = '';
            $.each(lst, function (i, option) {
                str += "<option value=\"" + option.Id + "\">" + option.Name + "</option>";
            });
            $('#selectProductOption').html(str);
        },
        error: function () {
            alert("error");
        }
    });
}
function addPromotionProductOption() {
    var optId = $('#selectProductOption').val();
    if (optId == "0" || optId == null) {
        optId = $('#hdProductOption').val();
        if (optId == "0" || optId == null || optId == "undefined") {
            alert("Please select a product option.");
            return false;
        }
    }
    var proId = $('#promotionId').val();
    App.blockUI($('body'));
    $.ajax({
        type: "POST",
        url: "/products/SavePromotionOption",
        data: { optionid: optId, promotionid: proId },
        success: function (data) {
            if (data.success == "0") {
                alert(data.message);
            } else {
                promotionselected(proId);
                App.unblockUI($('body'));
                alert(data.message);
            }
        },
        error: function () {
            App.unblockUI($('body'));
        }
    });
}
function deletePromoOption() {
    var id = $('#promooptionid').val();
    var promId = $('#promotionId').val();
    if (id == "") return false;
    $.ajax({
        url: "/products/DeletePromotionOption",
        type: "POST",
        data: { id: id },
        success: function (data) {
            promotionselected(promId);
        },
        error: function () {
            alert("error");
        }
    });
    return false;
}
function loadTreeSearch() {
    var ht = $('#treesearch').html();
    if (ht == "" || ht == null) {
        App.blockUI($('#treesearch'));
        $.ajax({
            url: "/products/StoreTree",
            success: function (data) {
                $('#treesearch').html(data);
                App.unblockUI($('#treesearch'));
            },
            error: function () {
                alert("error");
            }
        });
    }
    return false;
}
function loadPromotionList() {
    App.blockUI($('.promotionlist'));
    $.ajax({
        url: "/products/promotionlist",
        success: function (data) {
            $('.promotionlist').html(data);
            InitAjaxTable($('.tbl-promotion'));
            InitTableSelected();
            document.getElementById('ckShow').checked = false;
            App.unblockUI($('.promotionlist'));
        },
        error: function () {
            alert("error");
        }
    });
}

function addDepartment() {
    $("#department a.de-selected").each(function (index, element) {
        $('#departmentSelected').append("<a class='span12' rel='" + element.innerText + "' onclick='selected(this)'><span>" + element.innerText + "<span></a>");
        $(element).remove();
    });
}
function removeDepartment() {
    $("#departmentSelected a.de-selected").each(function (index, element) {
        $('#department').append("<a class='span12' rel='" + element.innerText + "' onclick='selected(this)'><span>" + element.innerText + "<span></a>");
        $(element).remove();
    });
}

function ChangeData() {
    App.blockUI($('body'));
    showDisplayName = $('#showDisplayName').is(":checked") ? true : false;
    storeId = $('#StoreId').val();
    $.ajax({
        url: "/Products/" + $('#activeTab').val() + "?storeId=" + storeId + "&showDisplayName=" + showDisplayName,
        success: function (data) {
            LoadData(data);

        }
    });
}
function ShowDetail(detail) {
    $('#webcateDetail>div').removeClass('active');
    $('#' + detail).addClass('active');
}

function ActiveTab(active) {
    $('#activeTab').val(active);
}


function LoadData(data) {
    if ($('#activeTab').val() == "ShowTreeView") {
        $('#treeviewcategory').html(data.listData);
        $("#tree").treeview({
            animated: "fast",
            collapsed: true,
        });
          $("#treeview p").click(function () {
            $("#treeview p").removeClass("tree-selected");
            $(this).addClass("tree-selected");
        });
        ShowStore(data.storeID);
    } else {
        $('#listviewcategory').html(data.listData);
         ShowCateDetail(data.cateID, data.storeID);
        $('#listviewcategory tr#' + data.cateID).addClass('row-selected');
    }
 
}
function ShowStore(storeID) {
    App.blockUI($('body'));
    ShowDetail('store');
    $.ajax({
        url: "/Products/ShowStoreDetail?storeId=" + storeID,
        success: function (data) {
            $('#store').html(data);
            App.unblockUI($('body'));
        }
    });
}
function ShowCateDetail(cateID, storeID) {
    ShowDetail('catedetail');
    var cateIDSelected = $('#categoryID').val();

    //$('#catedetail #' + cateID.toString()).addClass('li-selected');
    App.blockUI($('body'));
    $.ajax({
        url: "/Products/ShowCateDetail?cateID=" + cateID + "&cateIDSelected=" + cateIDSelected,
        success: function (data) {
            $('#catedetail').html(data);
            $('#searchproduct').attr("onclick", "FilterProducts('" + storeID + "','" + cateID + "')");
            $('#twebsite tr:first').removeClass('row-selected');
            $('#twebsite tr:first').addClass("row-selected");
            $('#btnAddParentCat').attr("onclick", "PopupCenter('/products/OpenParentCate?cateID=" + cateID + "&storeID=" + storeID + "',350,500)");
            $('#btnAddChildCat').attr("onclick", "PopupCenter('/products/OpenChildCate?cateID=" + cateID + "&storeID=" + storeID + "',350,500)");
            InitTabs();
            InitTableSelected();
            App.unblockUI($('body'));
        }
    });
}

function LoadSuperCateByWebsite(storeID, cateID) {
    App.blockUI($('body'));
    var displayProduct = $('#displayProduct').is(":checked");
    $.ajax({
        url: "/products/loadsupercatebywebsite?storeID=" + storeID + "&cateID=" + cateID + "&displayProduct=" + displayProduct,
        success: function (data) {
            document.getElementsByName('CategoryStoreID')[0].value = data.categoryStoreID;
            document.getElementsByName('displayName')[0].value = data.DisplayName;
            document.getElementsByName('categoryRewrite')[0].value = data.CategoryRewrite;
            document.getElementsByName('MetaPageTitle')[0].value = data.MetaPageTitle;
            document.getElementsByName('MetaDescription')[0].value = data.MetaDescription;
            document.getElementsByName('MetaKeywords')[0].value = data.MetaKeywords;
            document.getElementsByName('CustomBlock')[0].value = data.CustomBlock;
            document.getElementsByName('CustomBlock2')[0].value = data.CustomBlock2;
            $('#tproducts').html(data.DgvProducts);
            InitTableSelected();
            App.unblockUI($('body'));
        }
    });
    $('#searchproduct').attr("onclick", "FilterProducts('" + storeID + "','" + cateID + "')");
}

function ChangeDisplayProduct() {
    var displayProduct = $('#displayProduct').is(":checked");
    if (displayProduct) {
        $('#tproducts tr[class="displayproduct"]').hide();
    } else {
        $('#tproducts tr[class="displayproduct"]').show();
    }

}

function FilterProducts(storeID, cateID) {
    var displayProduct = $('#displayProduct').is(":checked");
    var filter = $('#filter').val();
    App.blockUI($('body'));
    $.ajax({
        url: "/products/FilterProducts?storeID=" + storeID + "&cateID=" + cateID + "&displayProduct=" + displayProduct + "&filter=" + filter,
        success: function (data) {
            $('#tproducts').html(data.DgvProducts);
            InitTableSelected();
            App.unblockUI($('body'));
        }
    });
}

function CurrentStore(storeID) {
    document.getElementsByName('StoreID')[0].value = storeID;
}
function Loadtwebsite(data) {

    $('#twebsite').html(data.twebsite);
    LoadSuperCateByWebsite(data.StoreID, data.CateID);
    CurrentStore(data.StoreID);

    InitTableSelected();
    $('#twebsite tr:first').removeClass('row-selected');
    $('#twebsite tr:first').addClass("row-selected");

}
function RemoveWebsite() {
    if (confirm("Are you sure you want to remove the category from this website?")) {
        var storeID = document.getElementsByName('StoreID')[0].value;
        var cateID = document.getElementsByName('categoryID')[0].value;
        App.blockUI($('body'));
        $.ajax({
            url: "/products/RemoveWebsite?storeID=" + storeID + "&cateID=" + cateID,
            success: function (data) {
                Loadtwebsite(data);
                InitTableSelected();
                App.unblockUI($('body'));
            },
            error: function () {
                App.unblockUI($('body'));
            }

        });
    }

}

function CurrentCateParent(cateID) {
    document.getElementsByName('parentCateID')[0].value = cateID;
}
function LoadParentCate(data) {
    $('#tparentcate').html(data.tparentCate);
    //CurrentCateParent(cateID);
    InitTableSelected();
}

function RemoveParentCate() {
    if (confirm("Are you sure you want to remove the category from this Parents?")) {
        var storeID = document.getElementsByName('StoreID')[0].value;
        var cateID = document.getElementsByName('categoryID')[0].value;
        var parentCateID = document.getElementsByName('parentCateID')[0].value;
        App.blockUI($('body'));
        $.ajax({
            url: "/products/RemoveParentCate?storeID=" + storeID + "&cateID=" + cateID + "&parentCateID=" + parentCateID,
            success: function (data) {
                LoadParentCate(data);
                App.unblockUI($('body'));
            },
            error: function () {
                App.unblockUI($('body'));
            }
        });
    }
}
function CurrentChildCate(cateID) {
    document.getElementsByName('childCateID')[0].value = cateID;
}
function LoadChildCate(data) {
    $('#tchildcate').html(data.tchildCate);
    InitTableSelected();
}
function RemoveChildCate() {
    if (confirm("Are you sure you want to remove the category from this Childrents?")) {
        var storeID = $('#StoreId').val();
        var cateID = document.getElementsByName('categoryID')[0].value;
        var childCateID = document.getElementsByName('childCateID')[0].value;
        App.blockUI($('body'));
        $.ajax({
            url: "/products/RemoveChildCate?storeID=" + storeID + "&cateID=" + cateID + "&childCateID=" + childCateID,
            success: function (data) {
                LoadChildCate(data);
                App.unblockUI($('body'));
            },
            error: function () {
                App.unblockUI($('body'));
            }
        });
    }
}

function SelectedProduct(productID) {
    $("#RemoveProduct").off('click');
    $("#RemoveProduct").on("click", function () {
        RemoveProduct(productID);
    });
    $("#RemovePosition").off('click');
    $("#RemovePosition").on("click", function () {
        RemovePosition(productID);
    });
    document.getElementsByName('selectedProduct')[0].value = productID;
}

function RemoveProduct(productID) {
    if (confirm("Are you sure you want to remove the product from this Products?")) {
        var storeID = document.getElementsByName('StoreID')[0].value;
        var cateID = document.getElementsByName('categoryID')[0].value;
        App.blockUI($('body'));
        $.ajax({
            url: "/products/RemoveProduct?storeID=" + storeID + "&productID=" + productID + "&cateID=" + cateID,
            success: function (data) {
                if (data.success == 1) {
                    $('#trproduct-' + productID).hide();
                }
                App.unblockUI($('body'));
                alert(data.message);
            },
            error: function () {
                App.unblockUI($('body'));
            }
        });
    }
}
function RemovePosition(productID) {
    var storeID = document.getElementsByName('StoreID')[0].value;
    var cateID = document.getElementsByName('categoryID')[0].value;
    var displayProduct = $('#displayProduct').is(":checked");
    App.blockUI($('body'));
    $.ajax({
        url: "/products/RemovePosition?storeID=" + storeID + "&productID=" + productID + "&cateID=" + cateID + '&displayProduct=' + displayProduct,
        success: function (data) {
            if (data.success == 1) {
                LoadProducts(data);
            }
            App.unblockUI($('body'));

        },
        error: function () {
            App.unblockUI($('body'));
        }
    });

}

function OpenProducts() {
    var cateID = document.getElementsByName('categoryID')[0].value;
    var storeID = document.getElementsByName('StoreID')[0].value;
    PopupCenter("/products/OpenProducts?storeID=" + storeID + "&cateID=" + cateID + "", 700, 550);
}

function GetDepartment(storeID) {
    $.ajax({
        url: "/products/GetDepartment?storeID=" + storeID,
        success: function (data) {
            $('#DepartmentID').html('');

            $('#DepartmentID').append("<option value=\"" + 0 + "\"> Select item....</option>");
            for (var i = 0; i < data.Departments.length; i++) {
                $('#DepartmentID').append("<option  value=\"" + data.Departments[i].Id + "\">" + data.Departments[i].Name + "</option>");
            }
        },
    });
}

function GetProductByDepartment(departID) {
    App.blockUI($('body'));
    $.ajax({
        url: "/products/GetProductByDepartment?departID=" + departID,
        success: function (data) {
            $('#tproducts').html(data.Prods);
            InitTableSelected();
            App.unblockUI($('body'));
        },
        error: function () {
            App.unblockUI($('body'));
        }
    });
    App.unblockUI($('body'));
}

function LoadProducts(data) {
    $('#tproducts').html(data.DgvProducts);
    InitTableSelected();

}

function OpenHistory() {

    var cateID = document.getElementsByName('categoryID')[0].value;
    var storeID = document.getElementsByName('StoreID')[0].value;
    PopupCenter("/products/OpenHistory?storeID=" + storeID + "&cateID=" + cateID + "", 600, 600);
}
function featureSelected(featureId) {
    App.blockUI($('#box-editfeature'));
    $.ajax({
        url: "/products/FeatureSelected",
        type: "POST",
        data: { featureId: featureId },
        success: function (data) {
            $('#box-editfeature').html(data);
            $("#fr-editfeature").submit(function () {
                var serializedForm = $(this).serialize();
                $.ajax({
                    type: 'POST',
                    url: '/products/SaveProductFeatures',
                    data: serializedForm,
                    success: function (data2) {
                        loadFeatureTable(data2);

                    },
                    error: function () {
                        alert('error!');
                    }
                });
                return false;
            });
            checkFeatureSections();
            App.unblockUI($('#box-editfeature'));
        },
        error: function () {
            alert("error");
            App.unblockUI($('#box-editfeature'));
        }
    });
}
function deleteFeature() {
    var featureid = $("#featureid").val();
    if (featureid != null) {
        if (confirm("Are you sure delete this feature?")) {
            $.ajax({
                type: 'POST',
                url: '/products/DeleteFeature?featureid=' + featureid,
                success: function (data2) {
                    loadFeatureTable(data2);

                },
                error: function () {
                    alert('error!');
                }
            });
        }
    } else {
        alert("Please choose a feature !!");
    }
}

function sectionSelected(sectionid, sectionname, sectiontag) {
    $("#sectionid").val(sectionid);
    $("#sectionname").val(sectionname);
    $("#sectiontag").val(sectiontag);
}

function deleteSection() {
    var sectionid = $("#sectionid").val();
    if (sectionid != null) {
        if (confirm("Are you sure delete this section?")) {
            App.blockUI($('body'));
            $.ajax({
                type: 'POST',
                url: '/products/DeleteFeatureSection?sectionid=' + sectionid,
                success: function (data2) {
                    $("body").html(data2);
                    App.unblockUI($('body'));
                    window.opener.reloadFeatureSection();
                },
                error: function () {
                    alert('error!');
                    App.unblockUI($('body'));
                }
            });
        }
    } else {
        alert("Please choose a section !!!");
    }

}
function addSection() {
    $("#sectionid").val("");
    $("#sectionname").val("");
    $("#sectiontag").val("");
    $(".tbl-section tbody tr").removeClass("row-selected");
    $("#sectionname").focus();
}

function reloadFeatureSection() {
    App.blockUI($('#feature-section'));
    $.ajax({
        type: 'POST',
        url: '/products/ReloadFeatureSection',
        success: function (data2) {
            $("#feature-section").html(data2);
            App.unblockUI($('#feature-section'));
        },
        error: function () {
            alert('error!');
            App.unblockUI($('#feature-section'));
        }
    });
}